package dao.impl;

import bean.StudentInfo;
import dao.IStudentInfoDao;
import utils.Conn;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StudentInfoDaoImpl implements IStudentInfoDao {
    Conn conn=new Conn();

    @Override
    public List<StudentInfo> queryStudentInfo() throws Exception {

        List<StudentInfo> list=new ArrayList<StudentInfo>();

        String sql="select * from student";

        ResultSet rs = conn.query(sql);

        while (rs.next()){

            int id = rs.getInt("id");

            String name = rs.getString("name");

            int age = rs.getInt("age");

            StudentInfo studentInfo=new StudentInfo();

            studentInfo.setId(id);
            studentInfo.setName(name);
            studentInfo.setAge(age);

            list.add(studentInfo);


        }


        return list;
    }

    @Override
    public boolean insertStudentInfo(StudentInfo studentInfo) throws Exception {

        int id = studentInfo.getId();

        String name = studentInfo.getName();

        int age = studentInfo.getAge();

        String sql="insert into student values("+id+",'"+name+"',"+age+")";

        return conn.insert(sql);
    }

    @Override
    public boolean updateStudentInfo(StudentInfo studentInfo) throws Exception {

        int id = studentInfo.getId();
        String name = studentInfo.getName();

        String sql="update student set name='"+name+"' where id="+id;

        return conn.update(sql);
    }

    @Override
    public boolean deleteStudentInfo(int id) throws Exception {

        String sql="delete from student where id="+id;

        return conn.del(sql);
    }

}